package RunnableOldClass;




import RunableSt.authenticator;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;
import java.util.Properties;


class Listner implements Runnable {   
static int count=1;static StringBuffer content = new StringBuffer("");
   int count1=0;public static int avg=0;
   int a=0;
public static String datetime;
  

    @Override
    public void run() {
          javax.swing.Timer WorkControl = new javax.swing.Timer(500, new SetWork());  
        WorkControl.start();
          // GETThread.jLabel4.setText(String.valueOf("Working..."));
        
        
        for(;;){ 
          content = new StringBuffer("");int count2=1;
         
         
          Calendar now = Calendar.getInstance();
            //int h = now.get(Calendar.HOUR_OF_DAY);
            int h = now.get(Calendar.HOUR_OF_DAY);
            int m = now.get(Calendar.MINUTE);
            int s = now.get(Calendar.SECOND);
            int i = now.get(Calendar.MILLISECOND);
           datetime =(h + ":" + m + ":" + s);
       
            try {
          //  URL sUrl = new URL("http://www.settrade.com/C13_MarketSummary.jsp");
           // System.out.println("sURL " + sUrl);
                
         String buffer;
          String url = "http://www.settrade.com/C13_MarketSummary.jsp",
               proxy = "proxy.up.ac.th",
               port = "8080",
               username = "up\\51286885",
               password = "7489250721";

            //URL autoIP = new URL("http://checkip.dyndns.org:8245/");
            //System.out.println("Get URL");
           
          URL server = new URL(url);

            Authenticator.setDefault(new authenticator(
                        username,password));
            Properties systemProperties = System.getProperties();
            systemProperties.setProperty("http.proxyHost",proxy);
            systemProperties.setProperty("http.proxyPort",port);
            HttpURLConnection connection = (HttpURLConnection)server.openConnection();
            connection.connect();
        // URLConnection conn = sUrl.openConnection();
            PrintStream out;
            try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"TIS-620"))) {
               
                out = new PrintStream(new FileOutputStream("SETINDEXFile.txt"));
                     content.append(datetime).append("\n");
                while((buffer = in.readLine()) != null) {
                       count2++;
                   // avg=(count2/count)*100;
                        
                       content.append(count2).append("-->").append(buffer).append("\n");
                       
                  }
            }
           //System.out.println("========================");
           //System.out.println(content.toString() + "\n");
            out.println(content.toString());
            out.close();
      //  GETINDEXSTOCK.jProgressBar1.setValue(25);
        
      
      //  GETINDEXSTOCK.jProgressBar1.setValue(100);
               
          // System.out.println("========================");
        } catch (Exception ez){
            System.err.println("Couldn't create help URL: " + ez);
        

    }// TODO add your handling code here:
           
                
            GETThread.jLabel1.setText(String.valueOf(count1));
     
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
        }
         ++a;
        count1++;
        }
    }
    
    
}
class SetWork implements ActionListener{
int count=1;
    @Override
    public void actionPerformed(ActionEvent ae) {
     //  System.out.println("--");
               
        if(count==1){
           GETThread.jLabel4.setText("Working."); 
           count++;
        }else if(count==2){
            GETThread.jLabel4.setText("Working.."); 
             count++;
        }else if(count==3){
            GETThread.jLabel4.setText("Working..."); 
            count=1;
        }
      
    }
    
    
    
    
}

       
        